package com.qf.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibProxy implements MethodInterceptor {
    private Object object;
    public CglibProxy(Object object){
        this.object = object;
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("写日志");
        method.invoke(object, objects);
        System.out.println("写日志");
        return null;
    }

    public Object getProxyInstance(){
        //1.创建字节码增强器
        Enhancer enhancer = new Enhancer();
        //2.指定为哪个类增强
        enhancer.setSuperclass(object.getClass());
        //3.增加额外功能
        enhancer.setCallback(this);
        //4.创建代理对象
        return enhancer.create();

    }
}
